<% /**
	* workoutsAddExercise.jsp
	* 
	* Contains the selection for input of Exercises for the inclusion into a workout object.
	*
	* @author Chris Avery
	*/
%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="b1w1nn1ng.crossf1t.db.*"%>
    <%@page import="b1w1nn1ng.crossf1t.gui.*"%>
    <%@page import="b1w1nn1ng.crossf1t.business.*"%>
    <%@page import="java.util.*"%>
    
    <% //this is a convienience variable: If it is true, then we have sufficient information to show a graph
    boolean ready = false; %>
	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
	<html>
	<%  
	User user = (User) session.getAttribute("user");
	session.setAttribute("user", user);

	Workout wo = (Workout) session.getAttribute("curWorkout");


	%>
	<head>
		<title> Workouts </title>
		<link rel="stylesheet" type="text/css" href="/crossfit/css/general.css" />
		<link rel="stylesheet" type="text/css" href="/crossfit/css/displayWorkout.css" />
	</head>
	<body>
	<div id="container">	
	<!-- This page deliberately contains no navbar: The back button ruins the series of forms -->
		<h1> Add Workout </h1> <hr />
		
		Please select the exercises that were part of your workout.
		
		<form action="./workoutsAddExercise.jsp" method="get">
		<select name="primary" onchange="this.form.submit();"> 
			<% 
			//get the different values and add them as select options
			String[] primaries = Database.getPrimarySeeList(false);
			
			//Print the "Categories" option at the top of the menu if we haven't selected one yet
			if(request.getParameter("primary") == null) {
				out.println("<option>Categories</option>");
			}
			
			//for each entry in the string array, prints it as an option in format:
			// <option value="entry">entry</option>
			for(String entry:primaries) {
				
				// If an option was selected last time, we do this check to keep it selected:
				if(request.getParameter("primary") != null) {
					if(request.getParameter("primary").equals(entry))
						out.println("<option selected value=\""+entry+"\">"+entry+"</option>");
					else
						out.println("<option value=\""+entry+"\">"+entry+"</option>");
				// Else, just print the options
				} else {
					out.println("<option value=\""+entry+"\">"+entry+"</option>");
				}
			}
			%>
		</select>
		<%
		
		// If a primary category was selected, we may have to display the secondary menu. Check
		// if we have to here:
		if(request.getParameter("primary") != null) {
			String[] secondary = Database.getSecondarySeeList(request.getParameter("primary"));
			
			//if the secondary seeList is null, we're done, there is no secondary.
			if(secondary == null) {
				ready = true;
			} else {
				//else, there is a secondary: here's the select menu for it.
				out.println("<select name=\"secondary\" onchange=\"this.form.submit();\">");
				//Print the "Excercise" option at the top of the menu if we haven't selected one yet
				//OR if primary changed since last time.
				if(request.getParameter("secondary") == null) {
					out.println("<option>Excercise</option>");
					ready = false;
				} else {
					boolean found = false;
					//check if primary changed by seeing if selected secondary is in the list still
					for(int i = 0; i < secondary.length; i++) {
						if(secondary[i].equals(request.getParameter("secondary"))) {
							found = true;
							ready = true;
							break;
						}
					}
					if(!found) {
						out.println("<option>Excercise</option>");
						ready = false;
					}
				}
				
				//for each entry in the string array, prints it as an option in format:
				// <option value="entry">entry</option>
				for(String entry:secondary) {
					
					// If an option was selected last time, we do this check to keep it selected:
					if(request.getParameter("secondary") != null) {
						if(request.getParameter("secondary").equals(entry))
							out.println("<option selected value=\""+entry+"\">"+entry+"</option>");
						else
							out.println("<option value=\""+entry+"\">"+entry+"</option>");
					// Else, just print the options
					} else {
						out.println("<option value=\""+entry+"\">"+entry+"</option>");
					}
				}
				out.println("</select>");
			}
		}
		%>		
		</form><br>
		
		<% 
		//THIS WHOLE SECTION IS FOR REFERENCE ONLY. The code here is to show how we access the needed fields.
		//This is not intended to remain in the final product and should be taken out when a graph is put in.
		out.println("A new graph "+(ready?"IS":"IS NOT")+" ready to be displayed.\n");
		if(ready) {
			//this is where we put the graphing goodness
			if(request.getParameter("secondary") != null)
				out.println("The page will display a graph on "+request.getParameter("secondary"));
			else
				out.println("The page will display a graph on "+request.getParameter("primary"));
		}
		%>
		
				<%

			%>
			<br />
			<br />
			<br />
	</div>
	</body>
</html>


